package com.bskj.common.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.LogUtils;

public abstract class BaseController {
    /**
     * 日志对象
     */
    protected Logger logger = Logger.getLogger(getClass());

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 得到response对象
     */
    public HttpServletResponse getResponse() {
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getResponse();
        return resp;
    }

    /**
     * 得到ModelAndView
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    // 返回查询操作结果
    public ModelAndView getModelAndView(Map returnMap) {
        ModelAndView mv = new ModelAndView();
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        view.setAttributesMap(returnMap);
        mv.setView(view);
        return mv;
    }

    public ModelAndView getRequestModelAndView() {
        ModelAndView mv = new ModelAndView();
        HttpServletRequest request = getRequest();
        LogUtils.debug(JSONObject.toJSONString(request.getParameterMap()));
        Map<String, String[]> extraParams = request.getParameterMap();
        for (String key : extraParams.keySet()) {
            mv.addObject(key, request.getParameter(key));
        }

        return mv;
    }


    public ModelAndView addParams(String paramsKey, String uri, Integer userId, ModelAndView mv) throws Exception {
        JSONObject params = new JSONObject();
        params.put("uri", uri);
        params.put("userId", userId);
        mv.addObject(paramsKey, EncryptUtil.encrypt(params.toJSONString()));

        return mv;
    }

    public ModelAndView addParams(String paramsKey, String uri, String custId, ModelAndView mv) throws Exception {
        JSONObject params = new JSONObject();
        params.put("uri", uri);
        params.put("custId", custId);
        mv.addObject(paramsKey, EncryptUtil.encrypt(params.toJSONString()));

        return mv;
    }

    public ModelAndView unpayDetailParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("unpayDetailParams", "/user/getUnpayDetail", userId, mv);
    }

    public ModelAndView topupParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("topupParams", "/user/recharge/topup", userId, mv);
    }

    public ModelAndView myMoneyDetailParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("myMoneyDetailParams", "/user/myMoneyDetail", userId, mv);
    }

    public ModelAndView withdrawCashParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("withdrawCashParams", "/user/recharge/withdrawCash", userId, mv);
    }

    public ModelAndView transDetailParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("transDetailParams", "/user/recharge/transDetail", userId, mv);
    }

    public ModelAndView addMyCarNoParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("addMyCarNoParams", "/userCar/addCarNo", userId, mv);
    }

    public ModelAndView delMyCarNoParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("delMyCarNoParams", "/userCar/delCarNo", userId, mv);
    }

    public ModelAndView bindCarNoParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("bindCarNoParams", "/userCar/bindCarNo", userId, mv);
    }

    public ModelAndView guidlineParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("guidlineParams", "/user/guidline", userId, mv);
    }

    public ModelAndView myCarNoParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("myCarNoParams", "/userCar/myCarNo", userId, mv);
    }

    public ModelAndView registerParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("registerParams", "/register/page", userId, mv);
    }

    public ModelAndView myMoneyParams(Integer userId, ModelAndView mv) throws Exception {
        return addParams("myMoneyParams", "/user/recharge/myMoney", userId, mv);
    }

}
